package com.example.administrator.officeplatform.Audit;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.bean.Fileinfo;
import com.example.administrator.officeplatform.kj.IpUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;
import cn.finalteam.toolsfinal.DeviceUtils;

import static com.example.administrator.officeplatform.R.id.imageView;

/**
 * Created by Administrator on 2017/12/27.
 */

public class ShowImageAdapter extends RecyclerView.Adapter {
    private List<Fileinfo> mList;
    private int mScreenWidth;

    public static interface OnRecyclerViewListener {
        void onItemClick(int position);
        boolean onItemLongClick(int position);
    }

    public ShowImageAdapter(Activity activity, List<Fileinfo> list) {
        this.mList = list;
        this.mScreenWidth = DeviceUtils.getScreenPix(activity).widthPixels;

    }
    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, null);
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                (mScreenWidth-40)/3,
                (mScreenWidth-40)/4);
        view.setLayoutParams(param);
        return new PersonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final PersonViewHolder holder1 = (PersonViewHolder) holder;

        ((PersonViewHolder) holder).position = position;

            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnFail(R.drawable.ic_gf_default_photo)
                    .showImageForEmptyUri(R.drawable.ic_gf_default_photo)
                    .showImageOnLoading(R.drawable.ic_gf_default_photo).build();
            Fileinfo photoInfo = mList.get(position);
//            ImageLoader.getInstance().displayImage("file:/" + photoInfo.getFileUrl(), holder1.ivPhoto, options);
            ImageLoader.getInstance().displayImage(IpUtil.getImage_address()+photoInfo.getFileUrl(),holder1.ivPhoto,options);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
        public int position;
        public ImageView ivPhoto;

        public PersonViewHolder(View itemView) {
            super(itemView);
            ivPhoto =(ImageView) itemView.findViewById(R.id.iv_photo);
            ivPhoto.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(position);
            }
            return false;
        }
    }

}
